package mis.ds.ps.model;

import mis.ds.ps.model.iface.Entities;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.poi.hssf.util.CellReference;

public class MapperLeaf implements Mapper {

	private static Logger logger = Logger.getLogger(MapperLeaf.class);

	private String name;
	private int sheet = -1;
	private CellReference startCell, stopCell;
	private Entities entityType;
	private String attribute, sheetName;
	private boolean acceptDuplicate;

	private String dbStartCell, dbEndCell;

	public MapperLeaf(String name) {

		PropertyConfigurator.configure("log4j.properties");

		this.name = name;
	}

	public boolean isValid() {

		if (stopCell.getCol() < startCell.getCol() || stopCell.getRow() < startCell.getRow()) {

			logger.debug("Mapper [" + name + "] is not valid");
			logger.debug("* StartCell[" + startCell.getRow() + "," + startCell.getCol() + "]");
			logger.debug("* EndCell[" + stopCell.getRow() + "," + stopCell.getCol() + "]");
			return false;
		}

		logger.debug("Mapper [" + name + "] is valid");
		return true;
	}

	public void setStartCell(int row, int col) {
		setStartCell(new CellReference(row, col));
	}

	public void setEndCell(int row, int col) {
		setStopCell(new CellReference(row, col));
	}

	public void setSheet(int sheet) {
		this.sheet = sheet;
	}

	public int getSheet() {
		return sheet;
	}

	public void setStartCell(CellReference startCell) {
		this.startCell = startCell;
	}

	public CellReference getStartCell() {
		return startCell;
	}

	public void setStopCell(CellReference stopCell) {
		this.stopCell = stopCell;
	}

	public CellReference getStopCell() {
		return stopCell;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public boolean isReadyForExecute() {

		// must have start and stop cell, sheet, entity and attribute

		System.out.println("#########Start" + startCell);
		System.out.println("#########stopCell" + stopCell);
		System.out.println("#########sheet" + sheet);
		System.out.println("#########sheetName" + sheetName);
		if (startCell == null || stopCell == null || (sheet < 0 && sheetName.equalsIgnoreCase(""))) {
			logger.debug("Mapper [" + name + "] is not ready for execution");
			return false;
		}

		logger.debug("Mapper [" + name + "] is ready for execution");
		return true;
	}

	public void setEntity(Entities entity) {
		this.entityType = entity;
	}

	public Entities getEntity() {
		return entityType;
	}

	public void setAttribute(String attribute) {
		this.attribute = attribute;
	}

	public String getAttribute() {
		return attribute;
	}

	public void setAcceptDuplicate(boolean acceptDuplicate) {
		this.acceptDuplicate = acceptDuplicate;
	}

	public boolean isAcceptDuplicate() {
		return acceptDuplicate;
	}

	public void setSheetName(String sheetName) {
		this.sheetName = sheetName;
	}

	public String getSheetName() {
		return sheetName;
	}

	public void setDbEndCell(String dbEndCell) {
		this.dbEndCell = dbEndCell;
	}

	public void setDbStartCell(String dbStartCell) {
		this.dbStartCell = dbStartCell;
	}

	public String getDbEndCell() {
		return dbEndCell;
	}

	public String getDbStartCell() {
		return dbStartCell;
	}

	@Override
	public String toString() {
		return "MapperLeaf [name=" + name + ", sheet=" + sheet + ", startCell=[" + startCell.getRow() + "," + startCell.getCol() + "], stopCell=["
				+ stopCell.getRow() + "," + stopCell.getCol() + "] , entityType=" + entityType + ", attribute=" + attribute + ", sheetName="
				+ sheetName + ", acceptDuplicate=" + acceptDuplicate + "]";
	}

}
